<?php
/**
 * Created by PhpStorm.
 * User: eric
 * Date: 2019/6/24
 * Time: 12:01
 */

namespace App\Http\Repositories\Redis;


use App\Util\Helper;

class BaseRedis
{

    //主键的key值 每个表可能不一样
    protected $idKey = 'id';


    /**
     * 获取 单个 节点数据
     * @param $id
     * @param array $aFields
     * @return array|bool
     */
    public function getNode($id, $aFields = array())
    {
        if (!$this->_redis || empty($id)) {
            return false;
        }
        $key = $this->prefix.":hash:" . $id;
        if (empty($aFields)) {
            $aReturn = $this->_redis->HGETAll($key);
        } else {
            if(!in_array($this->idKey, $aFields)){
                $aFields[] = $this->idKey;
            }
            $aReturn = $this->_redis->HMGET($key, $aFields);
        }
        if (empty($aReturn) || empty($aReturn[$this->idKey])) {//重新获取
            $data = $this->getDbNode($id);
            if (!empty($data)) {
                $this->_redis->HMSET($key, $data);
                $this->_redis->EXPIRE($key, $this->hashCacheTime);
                $aReturn = $aFields ? Helper::getArrayOfFields($data, $aFields) : $data;
            } else {
                $aReturn = false;
            }
        }
        return $aReturn;
    }


    /**
     * 更新哈希表数据
     * @param $id
     * @return bool|int
     */
    public function updateNode($id)
    {
        if (!$this->_redis) {
            return false;
        }
        if (!$id) {
            return false;
        }
        $key = $this->prefix.":hash:" . $id;
        $exist = $this->_redis->EXISTS($key);
        $ret = false;
        if ($exist) {
            $aData = $this->getDbNode([$id]);
//            $data = isset($aData[$id]) ? $aData[$id] : [];
            if($aData){
                $ret = $this->_redis->HMSET($key, $aData);
            }
        }
        return $ret ? true : false;
    }

    /**
     * 更新节点个别字段
     * @param $id
     * @param $aData
     * @return bool|int
     */
    public function updateNodeFields($id, $aData)
    {
        if (!$this->_redis) {
            return false;
        }
        if (!$id || empty($aData)) {
            return false;
        }
        $key = $this->prefix.":hash:" . $id;
        $exist = $this->_redis->HExists($key, $this->idKey);
        $ret = 0;
        if ($exist) {
            $ret = $this->_redis->HMSET($key, $aData);
        }
        return $ret;
    }

    /**
     *  删除哈希表
     * @param $id
     * @return bool|null
     */
    public function deleteHash($id)
    {
        if (!$this->_redis) {
            return false;
        }
        $key = $this->prefix.":hash:" . $id;
        $ret = $this->_redis->EXISTS($key);
        if ($ret) {
            return $this->_redis->DEL($key);
        } else {
            return null;
        }
    }

    /**
     * 没有的方法调redis的吧
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        $res = call_user_func_array(array($this->_redis, $name), $arguments);

        return $res;
    }

}
